"""Representation of various monsters"""

from pyre.core import dice
from pyre.data import info
from ruin import character
import ruin.player

class Monster(character.Character):
    """Base class for all computer controlled enemy characters"""

    def __init__(self, name, info):
        character.Character.__init__(self, info['face'], name, info['hp']())
        self.melee, self.weapon = info['hit']
        self.evade, self.armor = info['armor']
        self.speed = info['speed']

        self.alertness = -10
        self.perception = character.Skill(0)

    def act(self):
        self.stat_regen()

        player = self.world.get_actor(ruin.player.Player)

        if player:
            self.update_alertness(player)
            if self.alertness > 0:
                self.move(self.pos.direction_to(player.pos))
            elif self.alertness > -10:
                self.move(dice.rand_direction())

        return self.speed

    def update_alertness(self, player):
        old_alert = self.alertness
        penalty = self.pos.steps_to(player.pos)

        if self.perception.skill_roll(player.stealth, lambda x: x + penalty):
            self.alertness += 1

        if old_alert <= -10 and self.alertness > -10:
            self.append_message('%s <wake up>', self)
        if old_alert <= 0 and self.alertness > 0:
            self.append_message('%s <notice> %o', self, player)


def parse_monster_melee(data):
    skill_val, dice_roll = info.parse_melee(data)
    return character.Skill(skill_val), dice_roll


class MonsterInfo(info.InfoDB):
    """Parses a monster info data file"""

    def __init__(self, filename):
        info.InfoDB.__init__(self, filename, face=info.parse_tile,
                                             hp=info.parse_roll,
                                             hit=parse_monster_melee,
                                             armor=parse_monster_melee,
                                             speed=int)

    def create(self, name):
        return Monster(name, self[name])
